// DeletePingLunServlet.java - 删除评论
package com.example.li_han.servlet;

import com.example.li_han.service.PingLunService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "DeletePingLun", value = "/DeletePingLun")
public class DeletePingLun extends HttpServlet {
    private Gson gson = new Gson();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        BufferedReader br = req.getReader();
        StringBuilder sbuffer = new StringBuilder();
        String s;

        while((s = br.readLine()) != null) {
            sbuffer.append(s);
        }

        String requestBody = sbuffer.toString();
        System.out.println("收到删除评论请求: " + requestBody);

        Map<String, Object> resultMap = new HashMap<>();

        try {
            // 解析JSON数据
            Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
            Map<String, Object> rawData = gson.fromJson(requestBody, mapType);

            if (rawData == null) {
                resultMap.put("code", 500);
                resultMap.put("msg", "请求数据为空");
                sendResponse(resp, resultMap);
                return;
            }

            // 处理单个删除或批量删除
            if (rawData.containsKey("ids")) {
                // 批量删除
                Object idsObj = rawData.get("ids");
                List<Integer> ids = new ArrayList<>();

                if (idsObj instanceof List) {
                    List<?> rawList = (List<?>) idsObj;
                    for (Object item : rawList) {
                        if (item instanceof Double) {
                            ids.add(((Double) item).intValue());
                        } else if (item instanceof Integer) {
                            ids.add((Integer) item);
                        } else {
                            ids.add(Integer.parseInt(item.toString()));
                        }
                    }
                }

                PingLunService pingLunService = new PingLunService();
                int deletedCount = 0;
                for (Integer id : ids) {
                    int result = pingLunService.deleteComment(id);
                    if (result > 0) {
                        deletedCount++;
                    }
                }

                if (deletedCount > 0) {
                    resultMap.put("code", 200);
                    resultMap.put("msg", "成功删除 " + deletedCount + " 条评论");
                    resultMap.put("deletedCount", deletedCount);
                } else {
                    resultMap.put("code", 500);
                    resultMap.put("msg", "删除失败");
                }
            } else {
                // 单个删除
                if (!rawData.containsKey("pingLunId")) {
                    resultMap.put("code", 500);
                    resultMap.put("msg", "评论ID不能为空");
                    sendResponse(resp, resultMap);
                    return;
                }

                Object pingLunIdObj = rawData.get("pingLunId");
                int pingLunId;
                if (pingLunIdObj instanceof Double) {
                    pingLunId = ((Double) pingLunIdObj).intValue();
                } else if (pingLunIdObj instanceof Integer) {
                    pingLunId = (Integer) pingLunIdObj;
                } else {
                    pingLunId = Integer.parseInt(pingLunIdObj.toString());
                }

                PingLunService pingLunService = new PingLunService();
                int result = pingLunService.deleteComment(pingLunId);

                if (result > 0) {
                    resultMap.put("code", 200);
                    resultMap.put("msg", "删除成功");
                } else {
                    resultMap.put("code", 500);
                    resultMap.put("msg", "删除失败");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", 500);
            resultMap.put("msg", "服务器异常: " + e.getMessage());
        }

        sendResponse(resp, resultMap);
    }

    private void sendResponse(HttpServletResponse resp, Map<String, Object> resultMap) throws IOException {
        String json = gson.toJson(resultMap);
        resp.getWriter().write(json);
    }
}